

export function defineProperty(obj, key, value) {
  Object.defineProperty(obj, key, {
    value: value,
    enumerable: true,
    configurable: true,
    writable: true
  });
  return obj;
};

function defaultIntecepter(newValue, oldValue) {
  return newValue
}

export function g3x_defineProperty(target, propName, value, intecepter, observer) {
  // var _value = value;
  const isFn = (typeof(value) === "function");
  Object.defineProperty(target, propName, {
    get() {
      return isFn ? value() : value;
    },
    set(newValue) {
      const oldValue = value;
      if (oldValue !== newValue) {
        intecepter = intecepter || defaultIntecepter;
        value = intecepter(newValue, oldValue);
        if(value===undefined){
          value = newValue;
        }
        observer && observer(value, oldValue);
      }
    }
  })
}

export function g3x_defineReactProperty(target, propName, value, intecepter, observer) {
  // var _value = value;
  const isFn = (typeof(value) === "function");
  Object.defineProperty(target, propName, {
    get() {
      return isFn ? value() : value;
    },
    set(newValue) {
      const oldValue = value;
      if (oldValue !== newValue) {
        intecepter = intecepter || defaultIntecepter;
        value = intecepter(newValue, oldValue);
        if(value===undefined){
          value = newValue;
        }
        observer && observer(value, oldValue);
      }
    }
  })
}

export function g3x_defineReadonly(target, propName, value) {
  const isFn = (typeof(value) === "function");
  Object.defineProperty(target, propName, {
    get() {
      return isFn ? value() : value;
    },
  })
}

/////////////////////////////////////////////

function $defineProperties(target, props) {
  for (var i = 0; i < props.length; i++) {
    var descriptor = props[i];
    descriptor.enumerable = descriptor.enumerable || false;
    descriptor.configurable = true;
    if ("value" in descriptor) descriptor.writable = true;
    Object.defineProperty(target, descriptor.key, descriptor)
    // (0, _defineProperty2.default)(target, descriptor.key, descriptor);
  }
}

export function g3x_defineClassProperties(Constructor, protoProps, staticProps) {
  if (protoProps) $defineProperties(Constructor.prototype, protoProps);
  if (staticProps) $defineProperties(Constructor, staticProps);
  return Constructor;
}

/////////////////////////////////////////////

/**
 * @param {Object} obj
 * @param {Object} key
 * @param {Object} value
 * eg. const key = "polygonCreating"
 * eg. defineObjectProperty({}, key, false)
 */
export function defineObjectProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }
  return obj;
};

/////////////////////////////////////////////

/////////////////////////////////////////////
